﻿using AntUnion.FrameWork.Model.Models;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using AntUnion.FrameWork.Web.Extend;
using System;
using Util.Extension;
using Microsoft.AspNetCore.Mvc.RazorPages;
using TencentCloud.Tcr.V20190924.Models;
using MetaQuotes.MT5WebAPI.Common;
using TencentCloud.Cpdp.V20190820.Models;
using static MetaQuotes.MT5WebAPI.Common.MTOrder;
using System.Collections.Generic;
using AntUnion.FrameWork.Util;
using static MetaQuotes.MT5WebAPI.Common.MTPosition;

namespace AntUnion.FrameWork.Web.Service
{
    public class ReportServer
    {
        double volumeUnit = 10000;

        #region 挂单


        public class OrderList
        {
            public long orderNumber { get; set; }
            public string server { get; set; }
            public string Group { get; set; }
            public long tradingAccount { get; set; }
            public string name { get; set; }
            public int crmId { get; set; }
            public string CRMAccount { get; set; }
            public string login { get; set; }
            public string externalId { get; set; }
            public string symbol { get; set; }
            public long timeSetup { get; set; }
            public long state { get; set; }
            public string enOrderState { get; set; }
            public string enOrderType { get; set; }
            public string volume { get; set; }
            public long volumeInitial { get; set; }
            public long volumeCurrent { get; set; }
            public string price { get; set; }
            public string priceSl { get; set; }
            public string priceTp { get; set; }
            public string priceCurrent { get; set; }
            public long timeExpiration { get; set; }
        }

        public List<string> GetEnOrderType()
        {
            try
            {
                using (mtContext mtContext = new mtContext())
                {
                    var data = mtContext.Mt5Orders.OrderBy(k => k.EnOrderType).Select(k => k.EnOrderType).Distinct().ToList();
                    return data;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<string> GetOrderSymbol()
        {
            try
            {
                using (mtContext mtContext = new mtContext())
                {
                    var data = mtContext.Mt5Orders.OrderBy(k => k.Symbol).Select(k => k.Symbol).Distinct().ToList();
                    return data;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        ///  根据账户获取信息
        /// </summary>
        /// <param name="login"></param>
        /// <returns></returns>
        public List<OrderList> GetOrders(int page, int limit, string server, string group, int? crmId, string order, string symbol, string type, string time, out int count)
        {
            try
            {
                using (mtContext mtContext = new mtContext())
                {
                    var data = from o in mtContext.Mt5Orders
                               join mt5user in mtContext.Mt5Users
                               on o.Login equals mt5user.Login
                               join crmuser in mtContext.CrmUsers
                               on mt5user.CrmId equals crmuser.Id
                               select new OrderList
                               {
                                   orderNumber = o.Order,
                                   server = mt5user.Server,
                                   Group = mt5user.Group,
                                   tradingAccount = mt5user.Login,
                                   name = mt5user.Name,
                                   crmId = crmuser.Id,
                                   CRMAccount = crmuser.LoginName,
                                   externalId = o.ExternalId,
                                   symbol = o.Symbol,
                                   timeSetup = o.TimeSetup * 1000 - 8 * 3600 * 1000,
                                   state = o.State,
                                   enOrderState = o.EnOrderState,
                                   enOrderType = o.EnOrderType,
                                   volume = $"{(o.VolumeInitial / volumeUnit).ToString("F2")}/{(o.VolumeCurrent / volumeUnit).ToString("F2")}",
                                   volumeInitial = o.VolumeInitial,
                                   volumeCurrent = o.VolumeCurrent,
                                   price = o.PriceOrder.ToString("#,##0." + new string('0', (int)o.Digits)),
                                   priceSl = o.PriceSl.ToString("#,##0." + new string('0', (int)o.Digits)),
                                   priceTp = o.PriceTp.ToString("#,##0." + new string('0', (int)o.Digits)),
                                   priceCurrent = o.PriceCurrent.ToString("#,##0." + new string('0', (int)o.Digits)),
                                   timeExpiration = o.TimeExpiration * 1000 - 8 * 3600 * 1000
                               };
                    if (!string.IsNullOrEmpty(server))
                    {
                        data = data.Where(k => k.server == server);
                    }
                    if (!string.IsNullOrEmpty(group))
                    {
                        data = data.Where(k => k.Group == group);
                    }
                    if (crmId != null)
                    {
                        data = data.Where(k => k.crmId == crmId);
                    }
                    if (!string.IsNullOrEmpty(order))
                    {
                        data = data.Where(k => k.orderNumber.ToString().Contains(order));
                    }
                    if (!string.IsNullOrEmpty(symbol))
                    {
                        data = data.Where(k => k.symbol.Contains(symbol));
                    }
                    if (type != null)
                    {
                        data = data.Where(k => k.enOrderType == type);
                    }
                    if (!string.IsNullOrEmpty(time) && time.Contains(" - "))
                    {
                        string[] dateTimes = time.Split(new string[] { " - " }, StringSplitOptions.None);
                        //long startTime = Convert.ToInt64(dateTimes[0]);
                        //long endTime = Convert.ToInt64(dateTimes[1]);
                        long startTime = DateTimeUtil.GetTimeStamp(Convert.ToDateTime(dateTimes[0]));
                        long endTime = DateTimeUtil.GetTimeStamp(Convert.ToDateTime(dateTimes[1]));
                        data = data.Where(k => k.timeSetup > startTime && k.timeSetup < endTime);
                    }
                    count = data.Count();
                    List<OrderList> orderLists = data.OrderByDescending(k => k.timeSetup).Skip((page - 1) * limit).Take(limit).ToList();
                    return orderLists;
                }
            }
            catch (System.Exception)
            {
                throw;
            }
        }

        /// <summary>
        ///  根据账户获取信息
        /// </summary>
        /// <param name="login"></param>
        /// <returns></returns>
        public List<OrderList> GetDownOrders(int page, int limit, string server, string group, int crmId, string alllName,string order, string symbol, string type, string time, out int count)
        {
            try
            {
                using (mtContext mtContext = new mtContext())
                {
                    List<int> crmids = GetMyCustomers(mtContext, crmId);
                    var data = from o in mtContext.Mt5Orders
                               join mt5user in mtContext.Mt5Users
                               on o.Login equals mt5user.Login
                               join crmuser in mtContext.CrmUsers.Where(k => crmids.Contains(k.Id))
                               on mt5user.CrmId equals crmuser.Id
                               select new OrderList
                               {
                                   orderNumber = o.Order,
                                   server = mt5user.Server,
                                   Group = mt5user.Group,
                                   tradingAccount = mt5user.Login,
                                   name = mt5user.Name,
                                   crmId = crmuser.Id,
                                   CRMAccount = crmuser.LoginName,
                                   login = mt5user.Login.ToString(),
                                   externalId = o.ExternalId,
                                   symbol = o.Symbol,
                                   timeSetup = o.TimeSetup * 1000 - 8 * 3600 * 1000,
                                   state = o.State,
                                   enOrderState = o.EnOrderState,
                                   enOrderType = o.EnOrderType,
                                   volume = $"{(o.VolumeInitial / volumeUnit).ToString("F2")}/{(o.VolumeCurrent / volumeUnit).ToString("F2")}",
                                   volumeInitial = o.VolumeInitial,
                                   volumeCurrent = o.VolumeCurrent,
                                   price = o.PriceOrder.ToString("#,##0." + new string('0', (int)o.Digits)),
                                   priceSl = o.PriceSl.ToString("#,##0." + new string('0', (int)o.Digits)),
                                   priceTp = o.PriceTp.ToString("#,##0." + new string('0', (int)o.Digits)),
                                   priceCurrent = o.PriceCurrent.ToString("#,##0." + new string('0', (int)o.Digits)),
                                   timeExpiration = o.TimeExpiration * 1000 - 8 * 3600 * 1000
                               };
                    if (!string.IsNullOrEmpty(server))
                    {
                        data = data.Where(k => k.server == server);
                    }
                    if (!string.IsNullOrEmpty(group))
                    {
                        data = data.Where(k => k.Group == group);
                    }
                    if (!string.IsNullOrEmpty(alllName))
                    {
                        //data = data.Where(k => k.ownerFullName.Contains(fullName));
                        data = data.Where(k => (k.tradingAccount + "/" + k.CRMAccount + "/" + k.name).Contains(alllName));
                    }
                    if (!string.IsNullOrEmpty(order))
                    {
                        data = data.Where(k => k.orderNumber.ToString().Contains(order));
                    }
                    if (!string.IsNullOrEmpty(symbol))
                    {
                        data = data.Where(k => k.symbol.Contains(symbol));
                    }
                    if (type != null)
                    {
                        data = data.Where(k => k.enOrderType == type);
                    }
                    if (!string.IsNullOrEmpty(time) && time.Contains(" - "))
                    {
                        string[] dateTimes = time.Split(new string[] { " - " }, StringSplitOptions.None);
                        //long startTime = Convert.ToInt64(dateTimes[0]);
                        //long endTime = Convert.ToInt64(dateTimes[1]);
                        long startTime = DateTimeUtil.GetTimeStamp(Convert.ToDateTime(dateTimes[0]));
                        long endTime = DateTimeUtil.GetTimeStamp(Convert.ToDateTime(dateTimes[1]));
                        data = data.Where(k => k.timeSetup > startTime && k.timeSetup < endTime);
                    }
                    count = data.Count();
                    List<OrderList> orderLists = data.OrderByDescending(k => k.timeSetup).Skip((page - 1) * limit).Take(limit).ToList();
                    return orderLists;
                }
            }
            catch (System.Exception)
            {
                throw;
            }
        }

        #endregion

        #region 交易

        public class DealList
        {
            public long deal { get; set; }
            public string server { get; set; }
            public string Group { get; set; }
            public long tradingAccount { get; set; }
            public string name { get; set; }
            public int crmId { get; set; }
            public string CRMAccount { get; set; }
            public string login { get; set; }
            public string externalId { get; set; }
            public long orderNumber { get; set; }
            public string enDealAction { get; set; }
            public string enEntryFlags { get; set; }
            public string symbol { get; set; }
            public long time { get; set; }
            public string price { get; set; }
            public string volume { get; set; }
            public string profit { get; set; }
            public string storage { get; set; }
            public string profitRaw { get; set; }
            public string pricePosition { get; set; }


        }

        public List<string> GetDealAction()
        {
            try
            {
                using (mtContext mtContext = new mtContext())
                {
                    var data = mtContext.Mt5Deals.OrderBy(k => k.EnDealAction).Select(k => k.EnDealAction).Distinct().ToList();
                    return data;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<string> GetDealEntryFlag()
        {
            try
            {
                using (mtContext mtContext = new mtContext())
                {
                    var data = mtContext.Mt5Deals.OrderBy(k => k.EnEntryFlags).Select(k => k.EnEntryFlags).Distinct().ToList();
                    return data;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<string> GetDealSymbol()
        {
            try
            {
                using (mtContext mtContext = new mtContext())
                {
                    var data = mtContext.Mt5Deals.OrderBy(k => k.Symbol).Select(k => k.Symbol).Distinct().ToList();
                    return data;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<DealList> GetDeals(int page, int limit, string server, string group, int? crmId, string order, string symbol, string action, string entry, string time, out int count)
        {
            try
            {
                using (mtContext mtContext = new mtContext())
                {
                    var data = from o in mtContext.Mt5Deals
                               join mt5user in mtContext.Mt5Users
                               on o.Login equals mt5user.Login
                               join crmuser in mtContext.CrmUsers
                               on mt5user.CrmId equals crmuser.Id
                               select new DealList
                               {
                                   deal = o.Deal,
                                   server = mt5user.Server,
                                   Group = mt5user.Group,
                                   tradingAccount = mt5user.Login,
                                   name = mt5user.Name,
                                   crmId = crmuser.Id,
                                   CRMAccount = crmuser.LoginName,
                                   externalId = o.ExternalId,
                                   orderNumber = o.Order,
                                   enDealAction = o.EnDealAction,
                                   enEntryFlags = o.EnEntryFlags,
                                   symbol = o.Symbol,
                                   time = o.Time * 1000 - 8 * 3600 * 1000,
                                   price = o.Price.ToString("#,##0." + new string('0', (int)o.Digits)),
                                   volume = (o.Volume / volumeUnit).ToString("F2"),
                                   profit = o.Profit.ToString("#,##0.00"),
                                   storage = o.Storage.ToString("#,##0.00"),
                                   profitRaw = o.ProfitRaw.ToString("#,##0.00"),
                                   pricePosition = o.PricePosition.ToString("#,##0." + new string('0', (int)o.Digits))
                               };
                    data = data.Where(k => k.orderNumber != 0);
                    if (!string.IsNullOrEmpty(server))
                    {
                        data = data.Where(k => k.server == server);
                    }
                    if (!string.IsNullOrEmpty(group))
                    {
                        data = data.Where(k => k.Group == group);
                    }
                    if (crmId != null)
                    {
                        data = data.Where(k => k.crmId == crmId);
                    }
                    if (!string.IsNullOrEmpty(order))
                    {
                        data = data.Where(k => k.orderNumber.ToString().Contains(order));
                    }
                    if (!string.IsNullOrEmpty(symbol))
                    {
                        data = data.Where(k => k.symbol == symbol);
                    }
                    if (!string.IsNullOrEmpty(action))
                    {
                        data = data.Where(k => k.enDealAction == action);
                    }
                    if (!string.IsNullOrEmpty(entry))
                    {
                        data = data.Where(k => k.enEntryFlags == entry);
                    }
                    data = data.Where(k => k.orderNumber != 0);
                    if (!string.IsNullOrEmpty(time) && time.Contains(" - "))
                    {
                        string[] dateTimes = time.Split(new string[] { " - " }, StringSplitOptions.None);
                        //long startTime = Convert.ToInt64(dateTimes[0]);
                        //long endTime = Convert.ToInt64(dateTimes[1]);
                        long startTime = DateTimeUtil.GetTimeStamp(Convert.ToDateTime(dateTimes[0]));
                        long endTime = DateTimeUtil.GetTimeStamp(Convert.ToDateTime(dateTimes[1]));
                        data = data.Where(k => k.time > startTime && k.time < endTime);
                    }
                    count = data.Count();
                    List<DealList> orderLists = data.OrderByDescending(k => k.time).Skip((page - 1) * limit).Take(limit).ToList();
                    return orderLists;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        public List<DealList> GetDownDeals(int page, int limit, string server, string group, int crmId,string alllName, string order, string symbol, string action, string entry, string time, out int count)
        {
            try
            {
                using (mtContext mtContext = new mtContext())
                {
                    List<int> crmids = GetMyCustomers(mtContext, crmId);
                    var data = from o in mtContext.Mt5Deals
                               join mt5user in mtContext.Mt5Users
                               on o.Login equals mt5user.Login
                               join crmuser in mtContext.CrmUsers.Where(k => crmids.Contains(k.Id))
                               on mt5user.CrmId equals crmuser.Id
                               select new DealList
                               {
                                   deal = o.Deal,
                                   server = mt5user.Server,
                                   Group = mt5user.Group,
                                   tradingAccount = mt5user.Login,
                                   name = mt5user.Name,
                                   crmId = crmuser.Id,
                                   CRMAccount = crmuser.LoginName,
                                   login = mt5user.Login.ToString(),
                                   externalId = o.ExternalId,
                                   orderNumber = o.Order,
                                   enDealAction = o.EnDealAction,
                                   enEntryFlags = o.EnEntryFlags,
                                   symbol = o.Symbol,
                                   time = o.Time * 1000 - 8 * 3600 * 1000,
                                   price = o.Price.ToString("#,##0." + new string('0', (int)o.Digits)),
                                   volume = (o.Volume / volumeUnit).ToString("F2"),
                                   profit = o.Profit.ToString("#,##0.00"),
                                   storage = o.Storage.ToString("#,##0.00"),
                                   profitRaw = o.ProfitRaw.ToString("#,##0.00"),
                                   pricePosition = o.PricePosition.ToString("#,##0." + new string('0', (int)o.Digits))
                               };
                    data = data.Where(k => k.orderNumber != 0);
                    if (!string.IsNullOrEmpty(server))
                    {
                        data = data.Where(k => k.server == server);
                    }
                    if (!string.IsNullOrEmpty(group))
                    {
                        data = data.Where(k => k.Group == group);
                    }
                    if (!string.IsNullOrEmpty(alllName))
                    {
                        data = data.Where(k => (k.tradingAccount + "/" + k.CRMAccount + "/" + k.name).Contains(alllName));
                    }
                    if (!string.IsNullOrEmpty(order))
                    {
                        data = data.Where(k => k.orderNumber.ToString().Contains(order));
                    }
                    if (!string.IsNullOrEmpty(symbol))
                    {
                        data = data.Where(k => k.symbol == symbol);
                    }
                    if (!string.IsNullOrEmpty(action))
                    {
                        data = data.Where(k => k.enDealAction == action);
                    }
                    if (!string.IsNullOrEmpty(entry))
                    {
                        data = data.Where(k => k.enEntryFlags == entry);
                    }
                    data = data.Where(k => k.orderNumber != 0);
                    if (!string.IsNullOrEmpty(time) && time.Contains(" - "))
                    {
                        string[] dateTimes = time.Split(new string[] { " - " }, StringSplitOptions.None);
                        //long startTime = Convert.ToInt64(dateTimes[0]);
                        //long endTime = Convert.ToInt64(dateTimes[1]);
                        long startTime = DateTimeUtil.GetTimeStamp(Convert.ToDateTime(dateTimes[0]));
                        long endTime = DateTimeUtil.GetTimeStamp(Convert.ToDateTime(dateTimes[1]));
                        data = data.Where(k => k.time > startTime && k.time < endTime);
                    }
                    count = data.Count();
                    List<DealList> orderLists = data.OrderByDescending(k => k.time).Skip((page - 1) * limit).Take(limit).ToList();
                    return orderLists;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        #endregion

        #region 持仓

        public class PositionList
        {
            public long position { get; set; }
            public string server { get; set; }
            public string Group { get; set; }
            public long tradingAccount { get; set; }
            public string name { get; set; }
            public int crmId { get; set; }
            public string CRMAccount { get; set; }
            public string login { get; set; }
            public string symbol { get; set; }
            public string enPositionAction { get; set; }
            public long timeCreate { get; set; }
            public long timeUpdate { get; set; }
            public string priceOpen { get; set; }
            public string priceCurrent { get; set; }
            public string volume { get; set; }
            public string profit { get; set; }
            public string storage { get; set; }
            //public float rateProfit { get; set; }
            //public float rateMargin { get; set; }
        }

        public List<string> GetPositionAction()
        {
            try
            {
                using (mtContext mtContext = new mtContext())
                {
                    var data = mtContext.Mt5Positions.OrderBy(k => k.EnPositionAction).Select(k => k.EnPositionAction).Distinct().ToList();
                    return data;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<string> GetPositionSymbol()
        {
            try
            {
                using (mtContext mtContext = new mtContext())
                {
                    var data = mtContext.Mt5Positions.OrderBy(k => k.Symbol).Select(k => k.Symbol).Distinct().ToList();
                    return data;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<PositionList> GetPosition(int page, int limit, string server, string group, int? crmId, string position, string symbol, string action, string timeSetup, out int count)
        {
            try
            {
                using (mtContext mtContext = new mtContext())
                {
                    var data = from o in mtContext.Mt5Positions
                               join mt5user in mtContext.Mt5Users
                               on o.Login equals mt5user.Login
                               join crmuser in mtContext.CrmUsers
                               on mt5user.CrmId equals crmuser.Id
                               select new PositionList
                               {
                                   position = o.Position,
                                   server = mt5user.Server,
                                   Group = mt5user.Group,
                                   tradingAccount = mt5user.Login,
                                   name = mt5user.Name,
                                   crmId = crmuser.Id,
                                   CRMAccount = crmuser.LoginName,
                                   symbol = o.Symbol,
                                   enPositionAction = o.EnPositionAction,
                                   timeCreate = o.TimeCreate * 1000 - 8 * 3600 * 1000,
                                   timeUpdate = o.TimeUpdate * 1000 - 8 * 3600 * 1000,
                                   priceOpen = o.PriceOpen.ToString("#,##0." + new string('0', (int)o.Digits)),
                                   priceCurrent = o.PriceCurrent.ToString("#,##0." + new string('0', (int)o.Digits)),
                                   volume = (o.Volume / volumeUnit).ToString("F2"),
                                   profit = o.Profit.ToString("#,##0.00"),
                                   storage = o.Storage.ToString("#,##0.00"),
                                   //rateProfit = o.RateProfit,
                                   //rateMargin = o.RateMargin,
                               };
                    if (!string.IsNullOrEmpty(server))
                    {
                        data = data.Where(k => k.server == server);
                    }
                    if (!string.IsNullOrEmpty(group))
                    {
                        data = data.Where(k => k.Group == group);
                    }
                    if (crmId != null)
                    {
                        data = data.Where(k => k.crmId == crmId);
                    }
                    if (!string.IsNullOrEmpty(position))
                    {
                        data = data.Where(k => k.position.ToString().Contains(position));
                    }
                    if (!string.IsNullOrEmpty(symbol))
                    {
                        data = data.Where(k => k.symbol.Contains(symbol));
                    }
                    if (!string.IsNullOrEmpty(action))
                    {
                        data = data.Where(k => k.enPositionAction == action);
                    }
                    if (!string.IsNullOrEmpty(timeSetup) && timeSetup.Contains(" - "))
                    {
                        string[] dateTimes = timeSetup.Split(new string[] { " - " }, StringSplitOptions.None);
                        //long startTime = Convert.ToInt64(dateTimes[0]);
                        //long endTime = Convert.ToInt64(dateTimes[1]);
                        long startTime = DateTimeUtil.GetTimeStamp(Convert.ToDateTime(dateTimes[0]));
                        long endTime = DateTimeUtil.GetTimeStamp(Convert.ToDateTime(dateTimes[1]));
                        data = data.Where(k => k.timeCreate > startTime && k.timeCreate < endTime);
                    }
                    count = data.Count();
                    List<PositionList> positionList = data.OrderByDescending(k => k.timeCreate).Skip((page - 1) * limit).Take(limit).ToList();
                    return positionList;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<PositionList> GetDownPosition(int page, int limit, string server, string group, int crmId, string alllName,string position, string symbol, string action, string timeSetup, out int count)
        {
            try
            {
                using (mtContext mtContext = new mtContext())
                {
                    List<int> crmids = GetMyCustomers(mtContext,crmId);
                    var data = from o in mtContext.Mt5Positions
                               join mt5user in mtContext.Mt5Users
                               on o.Login equals mt5user.Login
                               join crmuser in mtContext.CrmUsers.Where(k => crmids.Contains(k.Id))
                               on mt5user.CrmId equals crmuser.Id
                               select new PositionList
                               {
                                   position = o.Position,
                                   server = mt5user.Server,
                                   Group = mt5user.Group,
                                   tradingAccount = mt5user.Login,
                                   name = mt5user.Name,
                                   crmId = crmuser.Id,
                                   CRMAccount = crmuser.LoginName,
                                   login = mt5user.Login.ToString(),
                                   symbol = o.Symbol,
                                   enPositionAction = o.EnPositionAction,
                                   timeCreate = o.TimeCreate * 1000 - 8 * 3600 * 1000,
                                   timeUpdate = o.TimeUpdate * 1000 - 8 * 3600 * 1000,
                                   priceOpen = o.PriceOpen.ToString("#,##0." + new string('0', (int)o.Digits)),
                                   priceCurrent = o.PriceCurrent.ToString("#,##0." + new string('0', (int)o.Digits)),
                                   volume = (o.Volume / volumeUnit).ToString("F2"),
                                   profit = o.Profit.ToString("#,##0.00"),
                                   storage = o.Storage.ToString("#,##0.00"),
                                   //rateProfit = o.RateProfit,
                                   //rateMargin = o.RateMargin,
                               };
                    if (!string.IsNullOrEmpty(server))
                    {
                        data = data.Where(k => k.server == server);
                    }
                    if (!string.IsNullOrEmpty(group))
                    {
                        data = data.Where(k => k.Group == group);
                    }
                    if (!string.IsNullOrEmpty(alllName))
                    {
                        data = data.Where(k => (k.tradingAccount + "/" + k.CRMAccount + "/" + k.name).Contains(alllName));
                    }
                    if (!string.IsNullOrEmpty(position))
                    {
                        data = data.Where(k => k.position.ToString().Contains(position));
                    }
                    if (!string.IsNullOrEmpty(symbol))
                    {
                        data = data.Where(k => k.symbol.Contains(symbol));
                    }
                    if (!string.IsNullOrEmpty(action))
                    {
                        data = data.Where(k => k.enPositionAction == action);
                    }
                    if (!string.IsNullOrEmpty(timeSetup) && timeSetup.Contains(" - "))
                    {
                        string[] dateTimes = timeSetup.Split(new string[] { " - " }, StringSplitOptions.None);
                        //long startTime = Convert.ToInt64(dateTimes[0]);
                        //long endTime = Convert.ToInt64(dateTimes[1]);
                        long startTime = DateTimeUtil.GetTimeStamp(Convert.ToDateTime(dateTimes[0]));
                        long endTime = DateTimeUtil.GetTimeStamp(Convert.ToDateTime(dateTimes[1]));
                        data = data.Where(k => k.timeCreate > startTime && k.timeCreate < endTime);
                    }
                    count = data.Count();
                    List<PositionList> positionList = data.OrderByDescending(k => k.timeCreate).Skip((page - 1) * limit).Take(limit).ToList();
                    return positionList;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        #endregion

        /// <summary>
        /// 获取我的二级下线
        /// </summary>
        /// <param name="mtContext"></param>
        /// <param name="crmId"></param>
        /// <returns></returns>
        public List<int> GetMyCustomers(mtContext mtContext, int crmId)
        {
            try
            {
                List<int> rmIdList = new List<int>();
                List<int> oneCrmId = mtContext.CrmUsers.Where(k => k.CrmUp == crmId).Select(k => k.Id).ToList();
                List<int> twoCrmId = mtContext.CrmUsers.Where(k => oneCrmId.Contains((int)k.CrmUp)).Select(k => k.Id).ToList();
                rmIdList.AddRange(oneCrmId);
                rmIdList.AddRange(twoCrmId);
                return rmIdList.Distinct().ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
